package graduation.project.store;

import graduation.project.body.TestCase;
import graduation.project.body.TestCases;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class FileTestCases implements ITestCases {

	public TestCases getTestCases(String tcsID) {
		TestCases tcs = null;
		try {
			String originExID = tcsID.substring(0, tcsID.indexOf('_'));
			File testSetFile = new File("D:\\data\\" + originExID + "\\testSet\\" + tcsID);
			BufferedReader in = new BufferedReader(new FileReader(testSetFile));
			ArrayList<String> contents = new ArrayList<String>();
			
			String s;			
			while( (s = in.readLine()) != null)
					contents.add(s);
			
			ArrayList<TestCase> testSet = new ArrayList<TestCase>();
			String originEx = (String)contents.get(1);
			String model = (String)contents.get(2);
			
			StringBuffer aLine = new StringBuffer();
			int testData;
			boolean expectantResult;
			for(int i = 3; i < contents.size(); i++){
				aLine.append(testSet.get(i));
				int colomPos = aLine.indexOf(":");
				testData = Integer.parseInt(aLine.substring(0, colomPos));
				if(aLine.substring(colomPos + 1, aLine.length()).equals("true"))
					expectantResult = true;
				else
					expectantResult = false;
				testSet.add(new TestCase(testData, expectantResult));
				aLine.delete(0, aLine.length());
			}
			
			tcs = new TestCases(originEx, tcsID, model);
			tcs.setTestSet(testSet);
			in.close();
		}catch(FileNotFoundException e) {
			System.out.println("The file does not exist");
			e.printStackTrace();
		}catch(IOException e){
			System.out.println("Occurring errors when reading the test cases file");
			e.printStackTrace();
		}
		return tcs;
	}

	public int insertTestCases(TestCases tcs) {
		if(tcs == null)
			return 0;
		int result = 0;
		try{
			String testSetID = tcs.getTestSetID();
			String originEx =tcs.getOriginEx();
			String model = tcs.getModel();
			ArrayList<TestCase> testSet = tcs.getTestSet();
			
			File dir = new File("D:\\data\\" + originEx.hashCode() + "\\testSet");
			dir.mkdirs();
			File testCasesFile = new File("D:\\data\\" + originEx.hashCode() + "\\testSet\\" + testSetID);
			if( ! testCasesFile.createNewFile()){
				System.out.println("the file exists.");
				return 0;
			}
			
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(testCasesFile)));
			out.println(testSetID);
			out.println(originEx);
			out.println(model);
			
			StringBuffer stringTestCase = new StringBuffer();
			TestCase aTest;
			for(int i = 0; i < testSet.size(); i++){
				aTest = testSet.get(i);
				stringTestCase.append(aTest.getTestData() + ":" + aTest.getExpectantResult());
				out.println(stringTestCase.toString());
				stringTestCase.delete(0, stringTestCase.length());
			}
			result = 1;
			out.close();
		}catch(IOException e){
			System.out.println("occurring error when write the file.");
			e.printStackTrace();
		}
			
		return result;
	}

	public int modifyTestCases(TestCases tcs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
